﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class statisticalList : MonoBehaviour
{
    public GameObject group;

    public GameObject prefab;

    private List<int[]> envCurData;
    public Button closeBtn;

    private ModeType _modeType;

    public void SetData(List<int[]> data, ModeType mode)
    {
        envCurData = data;
        _modeType = mode;
    }

    // Start is called before the first frame update
    void Start()
    {
        closeBtn.onClick.AddListener(() => { GameObject.Destroy(this.gameObject); });
        int max = 0;
        if (_modeType == ModeType.DaLeTou)
        {
            max = 35;
        }
        else
        {
            max = 33;
        }

        Dictionary<int, int> timesDictionary = new Dictionary<int, int>();

        for (int i = 1; i <= max; i++)
        {
            timesDictionary.Add(i, 0);
        }

        for (int i = 0; i < envCurData.Count; i++)
        {
            var data = envCurData[i];
            for (int j = 0; j < data.Length; j++)
            {
                timesDictionary[data[j]] = timesDictionary[data[j]] + 1;
            }
        }

        List<int> m_list = new List<int>();
        for (int i = 1; i <= max; i++)
        {
            m_list.Add(i);
        }

        m_list.Sort((a, b) => { return timesDictionary[a] - timesDictionary[b]; });


        for (int i = 0; i < m_list.Count; i++)
        {
            var obj = GameObject.Instantiate(prefab, @group.transform);
            obj.GetComponent<statisticalItem>().SetData(m_list[i], timesDictionary[m_list[i]]);
            obj.SetActive(true);
        }
    }

    // Update is called once per frame
    void Update()
    {
    }
}